angular.module("app").factory("zlInterceptor", ["utils", "$rootScope", "$q", function(utils, $rootScope, $q) {
	var res = 0
	var req = 0
	return {
		'request': function(config) {
			res = res + 1

			if(config.params) {
				if(config.params.header) {
					config.params = {
						report: yanqian(config.params)
					}
				}
			}

			function yanqian(arr) {
				var obj = arr
				var arr = JSON.stringify(arr)
				arr = arr.toLocaleLowerCase()
				arr = arr.split("")
				arr = arr.sort()
				arr = arr.join("")
				arr = arr + "8b5705fb1c7c0247a0b0506898526816"
				var hash = md5(arr)
				obj.header.sign = hash
				return obj
			}

			mui.showLoading("正在加载..", "div");
			return $q.resolve(config)
		},
		'response': function(response) {
			req = req + 1
			if(angular.isObject(response.data)) {
				response = response.data
				if(response.header) {
					if(response.header.re_code != "0000" && response.header.re_code != "8000"&& response.header.re_code != "2998") {
						utils.msg(response.header.re_msg)
					}
				}
			}
			if(req == res) {
				res = 0
				req = 0
				mui.hideLoading();
			}
			return $q.resolve(response);
		},
		'requestError': function(rejection) {
			res = 0
			req = 0
			mui.hideLoading();
			return $q.reject(rejection);
		},
		'responseError': function(rejection) {
			res = 0
			req = 0
			mui.hideLoading();
			utils.msg("服务端错误,错误码:" + rejection.status)
			return $q.reject(rejection)
		}

	}
}])